package com.dvhung.model.utils;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

import com.dvhung.common.ActionEvent;
import com.dvhung.constants.Constants;
import com.dvhung.controller.ItemController;
import com.dvhung.model.dto.ItemDTO;

public class ItemModel {
	private static ItemModel instance;
	public static ItemModel getInstance(){
		if(instance == null)
			instance = new ItemModel();
		return instance;
	}
	public void handleControllerEvent(ActionEvent e){
		switch (e.action) {
		case Constants.GET_ITEM:
			ArrayList<ItemDTO> newArr = getAllItem((Context) e.sender);
			e.viewData = newArr;
			ItemController.getInstance().handleModelViewEvent(e);
			break;

		default:
			break;
		}
	}
	
	public ArrayList<ItemDTO> getAllItem(Context context) {
		ArrayList<ItemDTO> item = new ArrayList<ItemDTO>();
		String sql = "SELECT* FROM ITEM";
		DBAcess db = new DBAcess(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			ItemDTO dto = new ItemDTO();
			dto.setItemName(c.getString(c.getColumnIndex("ItemName")));
			dto.setItemNo(c.getInt(c.getColumnIndex("ItemNo")));
			item.add(dto);
		}
		c.close();
		db.close();
		return item;
	}
}
